package com.pingan.haofang.searchcloud.api.query.parser.dbobject.sqlnode.aggregate;

import java.math.BigDecimal;

import com.pingan.haofang.searchcloud.api.query.parser.dbobject.FunctionConfig;

/**
 * ClassName: Avg <br>
 * Function:
 *
 * @author luyi
 * @version 1.0.0
 * @since 1.0.0
 * @date 20150816
 */
public class Avg extends AggregateFunction {

    private static final int VALUE_PARAM_COUNT = 2;

    public Avg() {
        setName(FunctionConfig.AVG.name);
    }

    @Override
    public Object getValue(Object...params) {
        assertParamCount(params, VALUE_PARAM_COUNT);
        assertParamType(params, Number.class);
        BigDecimal num1 = new BigDecimal(String.valueOf(params[0]));
        BigDecimal num2 = new BigDecimal(String.valueOf(params[1]));
        return num1.divide(num2, 2, BigDecimal.ROUND_HALF_EVEN);
    }

    @Override
    public int getParamCount() {
        return 1;
    }

}
